<?php

namespace App\Http\Controllers\OpenShop\Feishu;

use App\Enums\Check\CheckEnum;
use App\Http\Controllers\BaseController;
use App\Remote\Orm\OrmNewOrderRemote;
use App\Service\OpenShop\Feishu\KfosService;
use Illuminate\Http\Request;

/**
 * 首批订货
 */
class KfosController extends BaseController
{
    public function firstDeliveryNotes(string $orderNo)
    {
        return $this->success([
            'list' => KfosService::firstDeliveryNotes($orderNo),
        ]);
    }

    public function deliveryNoteReceived(Request $request)
    {
        $this->validate($request, [
            'orderNo' => 'required|string',
            'id'      => 'required|int',
        ]);
        $r = KfosService::deliveryNoteReceived($request->orderNo, $request->id);
        if ($r[0]) {
            return $this->success();
        }
        return $this->fail(400, $r[1]);
    }

    public function deliveryNoteDetail(Request $request)
    {
        $this->validate($request, [
            'orderNo' => 'required|string',
            'id' => 'required|int',
        ]);
        $data = KfosService::deliveryNoteDetail($request->orderNo, $request->id);
        if (null === $data) {
            return $this->fail(404, '出库单不存在');
        }
        return $this->success($data);
    }

    /**
     * 工服列表
     * @param Request $request
     */
    public function workClothesLists(Request $request)
    {
        $this->validate($request, [
            'orderNo' => 'required|string',
        ]);

        return $this->success(KfosService::workClothesLists($request->get('orderNo')));
    }

    /**
     * 新增工服订单
     * @param Request $request
     */
    public function createWorkClothes(Request $request)
    {
        $this->validate($request, [
            'orderNo'     => 'required|string',
            'goods'       => 'required|array',
            'goods.*.id'  => 'required|integer',
            'goods.*.num' => 'required|integer',
            //'supplierId'  => 'required|integer',
        ]);

        $orderNo    = $request->post('orderNo');
        $goods      = $request->post('goods');
        $supplierId = $request->post('supplierId', 0);
        $uid        = getRmsUserInfoByAuth()->id;
        $username   = getRmsUserInfoByAuth()->username;

        $newOrderId = KfosService::createWorkClothes($orderNo, $goods, $supplierId, $uid, $username);

        return $this->success(['newOrderId' => $newOrderId]);
    }

    /**
     * 关闭工服订单
     * @param Request $request
     */
    public function closeWorkClothes(Request $request)
    {
        $this->validate($request, [
            'orderNo'    => 'required|string',
            'newOrderId' => 'required|integer',
        ]);

        $orderNo    = (string)$request->post('orderNo');
        $newOrderId = (int)$request->post('newOrderId');
        $uid        = getRmsUserInfoByAuth()->id;
        $username   = getRmsUserInfoByAuth()->username;

        KfosService::closeNewOrder($orderNo, $newOrderId, $uid, $username, CheckEnum::SYS_TYPE_WORK_CLOTHES);

        return $this->success();
    }

    /**
     * 核销
     * @param Request $request
     */
    public function writeOff(Request $request)
    {
        $this->validate($request, [
            'orderNo'    => 'required|string',
            'newOrderId' => 'required|integer',
        ]);

        $orderNo    = (string)$request->post('orderNo');
        $newOrderId = (int)$request->post('newOrderId');

        KfosService::writeOffWorkClothes($orderNo, $newOrderId);

        return $this->success();
    }

}
